package com.example.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.ActivitySign;
import com.example.entity.Order;
import com.example.mapper.ActivitySignMapper;
import com.example.mapper.OrderMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 活动报名业务处理
 **/
@Service
public class orderService {

    @Resource
    private ActivitySignMapper acitvitySignMapper;

    @Resource
    private OrderMapper orderMapper;

    /**
     * 新增
     */
    public void add(Order order) {
//        order.setDate(DateUtil.today());
        // 看看用户有没有报名过
//        ActivitySign sign = orderMapper.selectByActivityIdAndUserId(order.getId(), order.getUserId());
//        if (sign != null) {
//            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
//        }
        orderMapper.insert(order);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        orderMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            orderMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Order order) {
        orderMapper.updateById(order);
    }

    /**
     * 根据ID查询
     */
    public Order selectById(Integer id) {
        return orderMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Order> selectAll(Order order) {
        return orderMapper.selectAll(order);
    }

    /**
     * 分页查询
     */
    public PageInfo<Order> selectPage(Order activitySign, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser.getRole().equals(RoleEnum.USER.name())) {  // 普通用户只查询自己的数据
            activitySign.setUserId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Order> list = orderMapper.selectAll(activitySign);
        return PageInfo.of(list);
    }

    public Order selectByActivityIdAndUserId(Integer activityId, Integer userId) {
        return orderMapper.selectByActivityIdAndUserId(activityId, userId);
    }

    public List<Dict> selectCount() {
//        List<Order> activitySigns = orderMapper.selectAll(null);
//        activitySigns = activitySigns.stream().filter(activitySign -> "审核通过".equals(activitySign.getStatus())
//                || "待审核".equals(activitySign.getStatus())).collect(Collectors.toList());
//        Set<String> set = activitySigns.stream().map(ActivitySign::getActivityName).collect(Collectors.toSet());
//        List<Dict> list = CollUtil.newArrayList();
//        for (String name : set) {
//            long count = activitySigns.stream().filter(activitySign -> activitySign.getActivityName().equals(name)).count();
//            Dict dict = Dict.create().set("name", name).set("value", count);
//            list.add(dict);
//        }
//        return list;
        return CollUtil.newArrayList();
    }
}